﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FoneBuddy.LinqModel;
using FoneBuddy.LinqData;
using FoneBuddy.LinqModel.Data;

public partial class Product_List : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
    Product product;
    List<Product> products = new List<Product>();
    ProductImageInfo pImg = new ProductImageInfo();
    List<ProductImageInfo> pImages = new List<ProductImageInfo>();

    #region pageload
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            getProduct();
    }
    #endregion

    #region getdata ItemCommand
    protected void ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            product = new Product(_dataObject);
            product.Id = Convert.ToInt32(e.CommandArgument);
            product.Delete();
            getProduct();
        }
    }
    private void getProduct()
    {
        product = new Product(_dataObject);
        pImg=new ProductImageInfo(_dataObject);
        pImages = pImg.GetAll();
        products = product.GetAll();
        

        if (products != null)
        {
            rptProduct.DataSource = products;
            rptProduct.DataBind();
        }
    }
    protected void OnDataBound(object Sender,RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {

            HiddenField hdfId = e.Item.FindControl("hdfId") as HiddenField;
            Int32 ProductId=Convert.ToInt32(hdfId.Value);
            Image img = e.Item.FindControl("productImg") as Image;
            if(pImages.Where(w=>w.ProductId==ProductId).ToList().Count>0)
            img.ImageUrl = "./ProductImages/"+pImages.Where(w=>w.ProductId==ProductId).FirstOrDefault().ImageName;
        }
    }
    #endregion
}